<?php
/**
 * Created by PhpStorm.
 * User: hantian
 * Date: 2017/11/17
 * Time: 下午4:38
 */

namespace App\Services;


use Iot\Request\V20170420\BatchGetDeviceStateRequest;
use Iot\Request\V20170420\PubRequest;
use Iot\Request\V20170420\QueryDeviceByNameRequest;
use Iot\Request\V20170420\QueryDeviceRequest;
use Iot\Request\V20170420\RegistDeviceRequest;
use Iot\Request\V20170420\RRpcRequest;
use Iot\Request\V20170420\UpdateDeviceShadowRequest;
use Iot\Request\V20170420\UpdateProductRequest;
use Iot\Request\V20170420\CreateProductRequest;
use Iot\Request\V20170420\GetDeviceShadowRequest;
use Iot\Request\V20170420\PubBroadcastRequest;

class AliService
{
    private $client = null;

    public function __construct()
    {
        $accessKeyId = "LTAIEUPuAc4yGvt2";
        $accessSecret = "RilYp52Bq8muJYwGfWDBphbxMrS8BU";
        $iClientProfile = \DefaultProfile::getProfile("cn-shanghai", $accessKeyId, $accessSecret);
        $this->client = new \DefaultAcsClient($iClientProfile);
    }

    /**创建产品
     * @param $CatId
     * @param $ProductName
     * @param $ProductDesc
     * @return mixed|\SimpleXMLElement
     */
    public function createProduct($CatId, $ProductName, $ProductDesc)
    {
        $request = new CreateProductRequest();
        $request->setCatId($CatId);
        $request->setDesc($ProductDesc);
        $request->setName($ProductName);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 修改产品
     * @param $input
     * @return mixed|\SimpleXMLElement
     */
    public function updateProduct($input)
    {
        $request = new UpdateProductRequest();
        $request->setProductKey($input['ProductKey']);
        if (!empty($input['CatId'])) {
            $request->setCatId($input['CatId']);
        }
        if (!empty($input['ProductName'])) {
            $request->setProductName($input['ProductName']);
        }
        if (!empty($input['ProductDesc'])) {
            $request->setProductDesc($input['ProductDesc']);
        }

        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 获取设备列表
     * @param $ProductKey
     * @param $PageSize
     * @param $CurrentPage
     * @return mixed|\SimpleXMLElement
     */
    public function getDeviceList($ProductKey, $PageSize, $CurrentPage)
    {
        $request = new QueryDeviceRequest();
        $request->setProductKey($ProductKey);
        $request->setCurrentPage($CurrentPage);
        $request->setPageSize($PageSize);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 注册设备
     * @param $ProductKey
     * @param $DeviceName
     * @return mixed|\SimpleXMLElement
     */
    public function deviceRegister($ProductKey, $DeviceName)
    {
        $request = new RegistDeviceRequest();
        $request->setProductKey($ProductKey);
        $request->setDeviceName($DeviceName);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 根据设备名称获取设备信息
     * @param $ProductKey
     * @param $DeviceName
     * @return mixed|\SimpleXMLElement
     */
    public function getDeviceDetail($ProductKey, $DeviceName)
    {
        $request = new QueryDeviceByNameRequest();
        $request->setProductKey($ProductKey);
        $request->setDeviceName($DeviceName);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 批量获取设备状态
     * @param $ProductKey
     * @param $DeviceName
     * @return mixed|\SimpleXMLElement
     */
    public function getDeviceStatus($ProductKey, $DeviceName)
    {
        $request = new BatchGetDeviceStateRequest();
        $request->setProductKey($ProductKey);
        $request->setDeviceNames($DeviceName);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }


    /**发布消息到topic
     * @param $ProductKey
     * @param $TopicFullName
     * @param $MessageContent
     * @param $Qos
     * @return mixed|\SimpleXMLElement
     */
    public function publishToTopic($ProductKey, $TopicFullName, $MessageContent, $Qos)
    {
        $request = new PubRequest();
        $request->setProductKey($ProductKey);
        $request->setTopicFullName($TopicFullName); //消息发送到的Topic全名.
        $request->setMessageContent($MessageContent); //hello world Base64 String.
        $request->setQos($Qos);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 给设备发消息
     * @param $ProductKey
     * @param $DeviceName
     * @param $RequestBase64Byte
     * @param $Timeout
     * @return mixed|\SimpleXMLElement
     */
    public function sendMessageToDevice($ProductKey, $DeviceName, $RequestBase64Byte, $Timeout)
    {
        $request = new RRpcRequest();
        $request->setProductKey($ProductKey);
        $request->setDeviceName($DeviceName);
        $request->setTimeOut($Timeout);
        $request->setRequestBase64Byte($RequestBase64Byte);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 发送广播消息
     * @param $ProductKey
     * @param $TopicFullName
     * @param $MessageContent
     * @return mixed|\SimpleXMLElement
     */
    public function sendBroadcastMessage($ProductKey, $TopicFullName, $MessageContent)
    {
        $request = new PubBroadcastRequest();
        $request->setProductKey($ProductKey);
        $request->setTopicFullName($TopicFullName); //消息发送到的Topic全名.
        $request->setMessageContent($MessageContent); //hello world Base64 String.
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 获取设备影子
     * @param $ProductKey
     * @param $DeviceName
     * @return mixed|\SimpleXMLElement
     */
    public function getDeviceShadow($ProductKey, $DeviceName)
    {
        $request = new GetDeviceShadowRequest();
        $request->setProductKey($ProductKey);
        $request->setDeviceName($DeviceName); //消息发送到的Topic全名.
        $response = $this->client->getAcsResponse($request);
        return $response;
    }

    /**
     * 更新影子信息
     * @param $ProductKey
     * @param $DeviceName
     * @param $ShadowMessage
     * @return mixed|\SimpleXMLElement
     */
    public function updateDeviceShadow($ProductKey, $DeviceName ,$ShadowMessage){
        $request = new UpdateDeviceShadowRequest();
        $request->setProductKey($ProductKey);
        $request->setDeviceName($DeviceName);
        $request->setShadowMessage($ShadowMessage);
        $response = $this->client->getAcsResponse($request);
        return $response;
    }
}